package com.qf.service;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsStorageMapper;
import com.qf.pojo.DtsStorage;
import com.qf.util.AliyunOSSUtil;
import com.qf.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author jiangjingbo
 * @version V1.0
 * @Project java2203
 * @Package com.qf.service
 * @Description:
 * @Date 2022/6/20 11:50
 */
@Service
public class StorageServiceimpl implements StorageService {
    @Autowired
    private DtsStorageMapper storageMapper;

    //阿里云北京服务器地址
    @Value("${aliyun.oss.endpoint}")
    private String endpoint;

    //子账户账号
    @Value("${aliyun.oss.accessKeyId}")
    private String accessKeyId;

    //子账户密码
    @Value("${aliyun.oss.accessKeySecret}")
    private String accessKeySecret;

    //bucket桶空间名字
    @Value("${aliyun.oss.bucketName}")
    private String bucketName;
    /**
     * 对象存储数据分页查询
     * @param key     根据文件的key进行查找
     * @param name   根据文件名字查找
     * @param page   当前页, 默认值1
     * @param limit  每页显示条数, 默认值10
     * @param sort   排序字段名, 默认按照add_time添加时间排序
     * @param order  排序方式, 默认desc降序
     */
    @Override
    public IPage<DtsStorage> queryStoragePage(String key, String name, Integer page, Integer limit, String sort, String order) {

        IPage<DtsStorage> page1 = new Page<>(page,limit);

        QueryWrapper<DtsStorage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted",false);
        if(!StringUtils.isEmpty(key)){
            queryWrapper.eq("key",key);
        }

        if(!StringUtils.isEmpty(name)){
            queryWrapper.like("name",name);
        }
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
        if (order.equalsIgnoreCase("desc")){
            //升序
            queryWrapper.orderByAsc(sort);
        }else {
            //降序
            queryWrapper.orderByDesc(sort);
        }
        }

        IPage<DtsStorage> storageIPage = storageMapper.selectPage(page1, queryWrapper);
        return storageIPage;
    }

    /**
     * 添加
     * @return
     */
    @Override
    public DtsStorage add(String name,Integer size , String substring, byte[] bytes) throws Exception{
        //生成随机字符串
        String key = UUID.randomUUID().toString();
        //拼接字符串
        String filename = name + substring;

        //调用 封装阿里云对象存储
        String url = AliyunOSSUtil.uploadImage(endpoint, accessKeyId, accessKeySecret,
                bucketName, filename, bytes);
        DtsStorage dtsStorage = new DtsStorage();
        dtsStorage.setAddTime(new Date());
        dtsStorage.setDeleted(false);
        dtsStorage.setUpdateTime(new Date());
        dtsStorage.setName(name);
        dtsStorage.setSize(size);
        dtsStorage.setUrl(url);
        dtsStorage.setKey(key);
        dtsStorage.setType(substring);

        storageMapper.insert(dtsStorage);

        return dtsStorage;
    }

    @Override
    public Integer update(DtsStorage storage) {
        storage.setUpdateTime(new Date());
        int i = storageMapper.updateById(storage);
        return i;

    }

    @Override
    public void delete(String key) {
       AliyunOSSUtil.deleteFile(endpoint, accessKeyId, accessKeySecret,
               bucketName ,key);
       QueryWrapper<DtsStorage> queryWrapper =new QueryWrapper();
       queryWrapper.eq("`key`",key);
       storageMapper.delete(queryWrapper);

    }


}
